<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2069 – To redirect stdout+stderr, `2>&1` must be last (or use `{ cmd > file; } 2>&1` to clarify).</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2069 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2069">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="to-redirect-stdoutstderr-21-must-be-last-or-use--cmd--file--21-to-clarify">To
redirect stdout+stderr, <code>2&gt;&amp;1</code> must be last (or use
<code>{ cmd &gt; file; } 2&gt;&amp;1</code> to clarify).</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2069.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">firefox</span> <span class="dv">2</span><span class="op">&gt;&amp;</span><span class="dv">1</span> <span class="op">&gt;</span> /dev/null</span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2069.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">firefox</span> <span class="op">&gt;</span> /dev/null <span class="dv">2</span><span class="op">&gt;&amp;</span><span class="dv">1</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>When it comes to redirection, order matters.</p>
<p>The problematic code means "Point stderr to where stdout is currently
pointing (the terminal). Then point stdout to /dev/null".</p>
<p>The correct code means "Point stdout to /dev/null. Then point stderr
to where stdout is currently pointing (/dev/null)".</p>
<p>In other words, the problematic code hides stdout and shows stderr.
The correct code hides both stderr and stdout, which is usually the
intention.</p>
<h3 id="exceptions">Exceptions</h3>
<p>If you actually do want to redirect stdout to a file, and then turn
stderr into the new stdout, you can make this more explicit with
braces:</p>
<pre><code>{ firefox &gt; /dev/null; } 2&gt;&amp;1</code></pre>
<p>Also note that this warning does not trigger when output is piped or
captured.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a href="https://mywiki.wooledge.org/BashPitfalls#pf43">Bash
Pitfalls: somecmd 2&gt;&amp;1 &gt;&gt;logfile</a></li>
<li><a href="https://mywiki.wooledge.org/BashFAQ/055">BashFaq: Tell me
all about 2&gt;&amp;1 -- what's the difference between 2&gt;&amp;1
&gt;foo and &gt;foo 2&gt;&amp;1, and when do I use which?</a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


